#include "warnaxisform.h"
#include "ui_warnaxisform.h"
#include"data.h"
#include<QMessageBox>

WarnAxisForm::WarnAxisForm(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::WarnAxisForm)
{
    ui->setupUi(this);



    connect(ui->pushButton_open,&QPushButton::clicked,this,[=](){
        if(ui->pushButton_open->text()=="开启"){
            keepOpen=true;
            ui->pushButton_open->setText("关闭");
            setIO();
            threadRun();

        }
        else{
            keepOpen=false;
            ui->pushButton_open->setText("开启");
        }
    });

    connect(this,&WarnAxisForm::SetTextSignal,this,&WarnAxisForm::SetTextSlot);

}
void WarnAxisForm::SetTextSlot(const QString&str,int i){
    if(i==0){
        ui->textEdit_logaxis0->insertPlainText(str);
    }else if(i==1){
        ui->textEdit_logaxis1->insertPlainText(str);
    }else if(i==2){
        ui->textEdit_logaxis2->insertPlainText(str);
    }else if(i==3){
        ui->textEdit_logaxis3->insertPlainText(str);
    }
}
WarnAxisForm::~WarnAxisForm()
{
    keepOpen=false;
    delete ui;
}


void WarnAxisForm::showEvent(QShowEvent *event){
    QList<QString> optionsIO;
    for(int j=0;j<40;++j){
        optionsIO.append(QString::number(j+1));
    }

    for(int i=0;i<4;++i){
        // 获取所有名为 "box_*" 的 ComboBox 控件
        QString comboBox=QString("comboBox_axis%1").arg(i);
        QComboBox *comboBoxs=findChild<QComboBox *>(comboBox);
        comboBoxs->addItems(optionsIO);
        int AlmIn=0;
        PMC_GetAlmIn(Data::handle,i,&AlmIn);
        comboBoxs->setCurrentText(QString::number(AlmIn));
    }
    //QMap<int,QString> warnMes;
    warnMes.insert(1, "随动误差超限告警\n");
    warnMes.insert(2, "与远程轴通讯出错\n");
    warnMes.insert(3, "远程驱动器报错\n");
    warnMes.insert(4, "正向硬限位\n");
    warnMes.insert(5, "反向硬限位\n");
    warnMes.insert(6, "找原点中\n");
    warnMes.insert(8, "随动误差超限出错\n");
    warnMes.insert(9, "超过正向软限位\n");
    warnMes.insert(10, "超过负向软限位\n");
    warnMes.insert(11, "CANCEL执行中\n");
    warnMes.insert(12, "脉冲频率超过MAX_SPEED限制.需要修改降速或修改MAX_SPEED\n");
    warnMes.insert(22, "告警信号输入\n");
    warnMes.insert(23, "轴进入了暂停状态\n");

    ui->textEdit_logaxis0->setReadOnly(true);
    ui->textEdit_logaxis1->setReadOnly(true);
    ui->textEdit_logaxis2->setReadOnly(true);
    ui->textEdit_logaxis3->setReadOnly(true);
}


void WarnAxisForm::setIO(){
    int axisIO0=ui->comboBox_axis0->currentText().toInt(&keepOpen,10);
    int axisIO1=ui->comboBox_axis1->currentText().toInt(&keepOpen,10);
    int axisIO2=ui->comboBox_axis2->currentText().toInt(&keepOpen,10);
    int axisIO3=ui->comboBox_axis3->currentText().toInt(&keepOpen,10);
    if(Data::handle!=nullptr){
        PMC_SetAlmIn(Data::handle,0,axisIO0);
        PMC_SetAlmIn(Data::handle,1,axisIO1);
        PMC_SetAlmIn(Data::handle,2,axisIO2);
        PMC_SetAlmIn(Data::handle,3,axisIO3);
        PMC_SetInvertIn(Data::handle,axisIO0,ui->checkBox_axis0->isChecked()?1:0);
        PMC_SetInvertIn(Data::handle,axisIO1,ui->checkBox_axis1->isChecked()?1:0);
        PMC_SetInvertIn(Data::handle,axisIO2,ui->checkBox_axis2->isChecked()?1:0);
        PMC_SetInvertIn(Data::handle,axisIO3,ui->checkBox_axis3->isChecked()?1:0);
    }
}

void WarnAxisForm::threadRun(){
    std::thread thr([&](){
        int errorNum=0;
        while (keepOpen) {
            for(int i=0;i<4;++i){

                PMC_GetAxisStatus(Data::handle,i,&errorNum);
                if(errorNum>0&&errorNum!=lastMesNum[i]){
                    QString message="";
                    for (int j = 0; j < 24; ++j) {
                        if((j>0&&j<6)||(j>7&&j<11)||j==12||j==22){
                            if(GetBitValue(errorNum,(ushort)j)){
                               emit SetTextSignal(timeNow.time().toString()+ warnMes[j],i);
                                message=warnMes[j];
                               lastMesNum[i]=errorNum;
                            }
                        }
                    }
                    // QMessageBox::information(this,tr("warn"),message);
                }
            }
        }
    });
    thr.detach();
}

bool WarnAxisForm::GetBitValue(int value,ushort index){
    ushort val=1<<index;
    return (value&val)==val;
}
